package com.springdoor.web.controller.goods;

import java.util.List;
import javax.servlet.http.HttpServletResponse;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.springdoor.common.annotation.Log;
import com.springdoor.common.core.controller.BaseController;
import com.springdoor.common.core.domain.AjaxResult;
import com.springdoor.common.enums.BusinessType;
import com.springdoor.goods.domain.SprdGoodsImg;
import com.springdoor.goods.service.ISprdGoodsImgService;
import com.springdoor.common.utils.poi.ExcelUtil;
import com.springdoor.common.core.page.TableDataInfo;

/**
 * 商品图片Controller
 * 
 * @author llshs
 * @date 2024-05-31
 */
@RestController
@RequestMapping("/goods/img")
public class SprdGoodsImgController extends BaseController
{
    @Autowired
    private ISprdGoodsImgService imgService;

    /**
     * 查询商品图片列表
     */
    @PreAuthorize("@ss.hasPermi('goods:image:list')")
    @GetMapping("/list")
    public TableDataInfo list(SprdGoodsImg sprdGoodsImg)
    {
        startPage();
        List<SprdGoodsImg> list = imgService.selectSprdGoodsImgList(sprdGoodsImg);
        return getDataTable(list);
    }

    /**
     * 查询商品图片列表
     */
    @PreAuthorize("@ss.hasPermi('goods:image:list')")
    @GetMapping(value = "/getImagesByGNumber/{gNumber}")
    public AjaxResult getImagesByGNumber(@PathVariable("gNumber") String gNumber)
    {
        return success(imgService.selectSprdGoodsImgListByGNumber(gNumber));
    }

    /**
     * 导出商品图片列表
     */
    @PreAuthorize("@ss.hasPermi('goods:image:export')")
    @Log(title = "商品图片", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, SprdGoodsImg sprdGoodsImg)
    {
        List<SprdGoodsImg> list = imgService.selectSprdGoodsImgList(sprdGoodsImg);
        ExcelUtil<SprdGoodsImg> util = new ExcelUtil<SprdGoodsImg>(SprdGoodsImg.class);
        util.exportExcel(response, list, "商品图片数据");
    }

    /**
     * 获取商品图片详细信息
     */
    @PreAuthorize("@ss.hasPermi('goods:image:query')")
    @GetMapping(value = "/{imgId}")
    public AjaxResult getInfo(@PathVariable("imgId") Long imgId)
    {
        return success(imgService.selectSprdGoodsImgByImgId(imgId));
    }

    /**
     * 新增商品图片
     */
    @PreAuthorize("@ss.hasPermi('goods:image:add')")
    @Log(title = "商品图片", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody SprdGoodsImg sprdGoodsImg)
    {
        sprdGoodsImg.setCreateBy(getUsername());
        sprdGoodsImg.setUpdateBy(getUsername());
        return toAjax(imgService.insertSprdGoodsImg(sprdGoodsImg));
    }

    /**
     * 修改商品图片
     */
    @PreAuthorize("@ss.hasPermi('goods:image:edit')")
    @Log(title = "商品图片", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody SprdGoodsImg sprdGoodsImg)
    {
        sprdGoodsImg.setUpdateBy(getUsername());
        return toAjax(imgService.updateSprdGoodsImg(sprdGoodsImg));
    }

    /**
     * 删除商品图片
     */
    @PreAuthorize("@ss.hasPermi('goods:image:remove')")
    @Log(title = "商品图片", businessType = BusinessType.DELETE)
	@DeleteMapping("/{imgIds}")
    public AjaxResult remove(@PathVariable Long[] imgIds)
    {
        return toAjax(imgService.deleteSprdGoodsImgByImgIds(imgIds));
    }

    @PreAuthorize("@ss.hasAnyPermi('goods:image:edit,goods:image:add')")
    @GetMapping("/findMaxOrderNum/{gNumber}")
    public AjaxResult findMaxOrderNum(@PathVariable String gNumber) {
        return success(imgService.findMaxOrderNum(gNumber));
    }
}
